home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / vim / src / alloc.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  4KB  |  235 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Read the file "credits.txt" for a list of people who contributed.
  6.  * Read the file "uganda.txt" for copying and usage conditions.
  7.  */
  8.  
  9. /*
  10.  * alloc.c
  11.  *
  12.  * This file contains various routines dealing with allocation and
  13.  * deallocation of memory. And some funcions for copying text.
  14.  */
  15.  
  16. #include "vim.h"
  17. #include "globals.h"
  18. #include "proto.h"
  19.  
  20. /*
  21.  * Some memory is reserved for error messages and for being able to
  22.  * call mf_release_all(), which needs some memory for mf_trans_add().
  23.  */
  24. #define KEEP_ROOM 8192L
  25.  
  26. /*
  27.  * Note: if unsinged is 16 bits we can only allocate up to 64K with alloc().
  28.  * Use lalloc for larger blocks.
  29.  */
  30.     char_u *
  31. alloc(size)
  32.     unsigned        size;
  33. {
  34.     return (lalloc((long_u)size, TRUE));
  35. }
  36.  
  37.     char_u *
  38. lalloc(size, message)
  39.     long_u            size;
  40.     int                message;
  41. {
  42.     register char_u   *p;            /* pointer to new storage space */
  43.     static int    releasing = FALSE;    /* don't do mf_release_all() recursive */
  44.     int            try_again;
  45.  
  46. #ifdef MSDOS
  47.     if (size >= 0xfff0)            /* in MSDOS we can't deal with >64K blocks */
  48.         p = NULL;
  49.     else
  50. #endif
  51.  
  52.     /*
  53.      * If out of memory, try to release some memfile blocks.
  54.      * If some blocks are released call malloc again.
  55.      */
  56.     for (;;)
  57.     {
  58.         if ((p = (char_u *)malloc(size)) != NULL)
  59.         {
  60.             if (mch_avail_mem(TRUE) < KEEP_ROOM && !releasing)
  61.             {                                 /* System is low... no go! */
  62.                     free((char *)p);
  63.                     p = NULL;
  64.             }
  65.         }
  66.     /*
  67.      * Remember that mf_release_all() is being called to avoid an endless loop,
  68.      * because mf_release_all() may call alloc() recursively.
  69.      */
  70.         if (p != NULL || releasing)
  71.             break;
  72.         releasing = TRUE;
  73.         try_again = mf_release_all();
  74.         releasing = FALSE;
  75.         if (!try_again)
  76.             break;
  77.     }
  78.  
  79.     /*
  80.      * Avoid repeating the error message many times (they take 1 second each).
  81.      * Did_outofmem_msg is reset when a character is read.
  82.      */
  83.     if (message && p == NULL && !did_outofmem_msg)
  84.     {
  85.         emsg(e_outofmem);
  86.         did_outofmem_msg = TRUE;
  87.     }
  88.     return (p);
  89. }
  90.  
  91. /*
  92.  * copy a string into newly allocated memory
  93.  */
  94.     char_u *
  95. strsave(string)
  96.     char_u           *string;
  97. {
  98.     char_u *p;
  99.  
  100.     p = alloc((unsigned) (STRLEN(string) + 1));
  101.     if (p != NULL)
  102.         STRCPY(p, string);
  103.     return p;
  104. }
  105.  
  106.     char_u *
  107. strnsave(string, len)
  108.     char_u        *string;
  109.     int         len;
  110. {
  111.     char_u *p;
  112.  
  113.     p = alloc((unsigned) (len + 1));
  114.     if (p != NULL)
  115.     {
  116.         STRNCPY(p, string, (size_t)len);
  117.         p[len] = NUL;
  118.     }
  119.     return p;
  120. }
  121.  
  122. /*
  123.  * copy a number of spaces
  124.  */
  125.     void
  126. copy_spaces(ptr, count)
  127.     char_u    *ptr;
  128.     size_t    count;
  129. {
  130.     register size_t    i = count;
  131.     register char_u    *p = ptr;
  132.  
  133.     while (i--)
  134.         *p++ = ' ';
  135. }
  136.  
  137. /*
  138.  * delete spaces at the end of the string
  139.  */
  140.     void
  141. del_spaces(ptr)
  142.     char_u *ptr;
  143. {
  144.     char_u    *q;
  145.  
  146.     q = ptr + STRLEN(ptr);
  147.     while (--q > ptr && isspace(q[0]) && q[-1] != '\\' && q[-1] != Ctrl('V'))
  148.         *q = NUL;
  149. }
  150.  
  151. #ifdef NO_FREE_NULL
  152. #undef free
  153. /*
  154.  * replacement for free() that cannot handle NULL pointers
  155.  */
  156.     void
  157. nofreeNULL(x)
  158.     void *x;
  159. {
  160.     if (x != NULL)
  161.         free(x);
  162. }
  163. #endif
  164.  
  165. #ifdef BSD_UNIX
  166.     char *
  167. bsdmemset(ptr, c, size)
  168.     char    *ptr;
  169.     int        c;
  170.     long    size;
  171. {
  172.     register char *p = ptr;
  173.  
  174.     while (size-- > 0)
  175.         *p++ = c;
  176.     return ptr;
  177. }
  178. #endif
  179.  
  180. #ifdef MEMMOVE
  181. /*
  182.  * Version of memmove that handles overlapping source and destination.
  183.  * For systems that don't have a function that is guaranteed to do that (SYSV).
  184.  */
  185.     void *
  186. #ifdef __sgi
  187. memmove(desti, source, len)
  188.     void    *source, *desti;
  189.     size_t    len;
  190. #else
  191. memmove(desti, source, len)
  192.     void    *source, *desti;
  193.     int        len;
  194. #endif
  195. {
  196.     char    *src = (char *)source;
  197.     char    *dst = (char *)desti;
  198.  
  199.     if (dst > src && dst < src + len)    /* overlap, copy backwards */
  200.     {
  201.         src +=len;
  202.         dst +=len;
  203.         while (len-- > 0)
  204.             *--dst = *--src;
  205.     }
  206.     else                                /* copy forwards */
  207.         while (len-- > 0)
  208.             *dst++ = *src++;
  209.     return desti;
  210. }
  211. #endif
  212.  
  213. /*
  214.  * compare two strings, ignoring case
  215.  * return 0 for match, 1 for difference
  216.  */
  217.     int
  218. vim_strnicmp(s1, s2, len)
  219.     char_u    *s1;
  220.     char_u    *s2;
  221.     size_t    len;
  222. {
  223.     while (len)
  224.     {
  225.         if (TO_UPPER(*s1) != TO_UPPER(*s2))
  226.             return 1;                        /* this character different */
  227.         if (*s1 == NUL)
  228.             return 0;                        /* strings match until NUL */
  229.         ++s1;
  230.         ++s2;
  231.         --len;
  232.     }
  233.     return 0;                                /* strings match */
  234. }
  235.